package com.ccy.product.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.ccy.common.core.domain.Result;
import com.ccy.common.core.utils.DateUtils;
import com.ccy.product.domain.*;
import com.ccy.product.mapper.*;
import com.ccy.search.domain.Attrs;
import com.ccy.search.domain.SkuEsModel;
import com.ccy.search.feign.SearchFeignService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ccy.product.service.IPmsSpuInfoService;

/**
 * spu信息Service业务层处理
 *
 * @author ccy
 * @date 2024-01-16
 */
@Service
public class PmsSpuInfoServiceImpl implements IPmsSpuInfoService
{
    @Autowired
    private PmsSpuInfoMapper pmsSpuInfoMapper;


    @Autowired
    private PmsSkuInfoMapper pmsSkuInfoMapper;

    @Autowired
    private PmsBrandMapper pmsBrandMapper;

    @Autowired
    private PmsCategoryMapper pmsCategoryMapper;

    @Autowired
    private PmsProductAttrValueMapper pmsProductAttrValueMapper;


    @Autowired
    private SearchFeignService searchFeignService;


    /**
     * 查询spu信息
     *
     * @param id spu信息主键
     * @return spu信息
     */
    @Override
    public PmsSpuInfo selectPmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoById(id);
    }

    /**
     * 查询spu信息列表
     *
     * @param pmsSpuInfo spu信息
     * @return spu信息
     */
    @Override
    public List<PmsSpuInfo> selectPmsSpuInfoList(PmsSpuInfo pmsSpuInfo)
    {
        return pmsSpuInfoMapper.selectPmsSpuInfoList(pmsSpuInfo);
    }

    /**
     * 新增spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int insertPmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setCreateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.insertPmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 修改spu信息
     *
     * @param pmsSpuInfo spu信息
     * @return 结果
     */
    @Override
    public int updatePmsSpuInfo(PmsSpuInfo pmsSpuInfo)
    {
        pmsSpuInfo.setUpdateTime(DateUtils.getNowDate());
        return pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);
    }

    /**
     * 批量删除spu信息
     *
     * @param ids 需要删除的spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoByIds(Long[] ids)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoByIds(ids);
    }

    /**
     * 删除spu信息信息
     *
     * @param id spu信息主键
     * @return 结果
     */
    @Override
    public int deletePmsSpuInfoById(Long id)
    {
        return pmsSpuInfoMapper.deletePmsSpuInfoById(id);
    }

    @Override
    public void up(Long id) throws IOException {

        //1. 根据spuId获取sku列表
        List<PmsSkuInfo> pmsSkuInfos = getSkuInfoListBySpuId(id);


        //2. skuInfos -->  skuEsModels
        List<SkuEsModel> skuEsModelList = transferSkuEsModelList(pmsSkuInfos,id);


        //3. 远程调用导入es
        Result result = searchFeignService.productStatusUp(skuEsModelList);


        if (result.getCode()==200){

            PmsSpuInfo pmsSpuInfo = this.pmsSpuInfoMapper.selectPmsSpuInfoById(id);
            pmsSpuInfo.setPublishStatus(1);
            pmsSpuInfoMapper.updatePmsSpuInfo(pmsSpuInfo);


        }





    }


    //skuList  -->  skuEsModelList
    private List<SkuEsModel> transferSkuEsModelList(List<PmsSkuInfo> pmsSkuInfos,Long id) {


        //处理attrs
        List<PmsProductAttrValue> pmsProductAttrValues = pmsProductAttrValueMapper.selectSearchableBySpuInfoId(id);
        List<Attrs> attrsList = pmsProductAttrValues.stream().map(attrvalue -> {
            Attrs attrs = new Attrs();
            attrs.setAttrId(attrvalue.getAttrId());
            attrs.setAttrName(attrvalue.getAttrName());
            attrs.setAttrValue(attrvalue.getAttrValue());

            return attrs;


        }).collect(Collectors.toList());

        return pmsSkuInfos.stream().map(skuInfo -> {

            SkuEsModel skuEsModel = new SkuEsModel();
            //1. skuId
            skuEsModel.setSkuId(skuInfo.getSkuId());

            //2. spuId
            skuEsModel.setSpuId(skuInfo.getSpuId());

            //3. skuTitle
            skuEsModel.setSkuTitle(skuInfo.getSkuTitle());

            skuEsModel.setSkuPrice(skuInfo.getPrice());

            skuEsModel.setSkuImg(skuInfo.getSkuDefaultImg());

            skuEsModel.setSaleCount(skuInfo.getSaleCount());

            skuEsModel.setHasStock(true);

            skuEsModel.setHotScore(Long.valueOf(100));

            PmsBrand brand = pmsBrandMapper.selectPmsBrandByBrandId(skuInfo.getBrandId());
            skuEsModel.setBrandId(brand.getBrandId());
            skuEsModel.setBrandName(brand.getName());
            skuEsModel.setBrandImg(brand.getLogo());

            PmsCategory category = pmsCategoryMapper.selectPmsCategoryByCatId(skuInfo.getCatalogId());
            skuEsModel.setCatalogId(category.getCatId());
            skuEsModel.setCatalogName(category.getName());




            skuEsModel.setAttrs(attrsList);


            return skuEsModel;
        }).collect(Collectors.toList());


    }


    /**
     * 根据spuId获取skuList
     * @param id
     * @return
     */
    private List<PmsSkuInfo> getSkuInfoListBySpuId(Long id) {

        PmsSkuInfo pmsSkuInfo = new PmsSkuInfo();
        pmsSkuInfo.setSpuId(id);
        List<PmsSkuInfo> pmsSkuInfos = pmsSkuInfoMapper.selectPmsSkuInfoList(pmsSkuInfo);

        return pmsSkuInfos;
    }
}
